package com.woniu.cms.contract.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.cms.contract.core.entity.AreaVo;
import com.woniu.cms.contract.core.entity.FrameworkContract;
import com.woniu.cms.contract.core.entity.UndertakeContract;
import com.woniu.cms.contract.core.mapper.UndertakeContractMapper;
import com.woniu.cms.contract.core.service.IBidService;
import com.woniu.cms.contract.core.service.IUndertakeContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.api.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author maple leaf
 * @since 2022-09-17
 */
@Service
public class UndertakeContractServiceImpl extends ServiceImpl<UndertakeContractMapper, UndertakeContract> implements IUndertakeContractService {

    @Autowired
    private UndertakeContractMapper undertakeContractMapper;

    @Autowired
    private IBidService bidService;

    @Override
    public Result queryUnder(String undertakeContractCode, Integer currentPage, Integer pageSize) {

        Page<UndertakeContract> page = new Page<>();
        QueryWrapper<UndertakeContract> wrapper = new QueryWrapper<>();
        if (undertakeContractCode != null && undertakeContractCode != ""){
            wrapper.like("undertake_contract_code",undertakeContractCode);
        }
        Page<UndertakeContract> contractPage = this.page(page, wrapper);
        List<UndertakeContract> records = contractPage.getRecords();
        long total = contractPage.getTotal();

        HashMap<String, Object> map = new HashMap<>();
        map.put("records",records);
        map.put("total",total);
        return Result.common(map);
    }

    @Override
    public Result addUnder(UndertakeContract undertakeContract) {

            undertakeContract.setSubmitTime(new Date());

            //设置时间格式，截取时间年份
            SimpleDateFormat format = new SimpleDateFormat("yyyy");
            //将时间转成String
            String date = format.format(new Date());
            //根据用户输入地区，查询地区的编号
            String region = undertakeContract.getRegion();
            AreaVo areaVo = bidService.areaName(region);

            //截取编号后四位
            String substring = areaVo.getAreaId().substring(2, 6);

            //使用年份+编号进行模糊查询，得到list集合
        List<UndertakeContract> contracts = this.generatedNumber("UN", date, substring);

        //判断集合是否有数据
            if (contracts.size() == 0){
                //数据库没有数据则自添一条
                undertakeContract.setUndertakeContractCode("UN" + date + substring + "001");
                System.out.println(undertakeContract.getUndertakeContractCode());
            }else{
                //拿到集合的第一条数据
                UndertakeContract contract = contracts.get(0);
                System.out.println("contract = " + contract);

                //拿到数据中最新的投标编号
                String unCode = contract.getUndertakeContractCode();
                System.out.println("code = " + unCode);
                //截取字段进行增加
                String substring1 = unCode.substring(6, unCode.length());
                Long i = Long.valueOf(Integer.parseInt(substring1));
                ++i;
                System.out.println("i = " + i);
                String s = String.valueOf(i);
                //截取字段，进行合并，添加到对象中
                String substring2 = unCode.substring(0, 7);
                String s2 = substring2 + s;
                System.out.println("s2 = " + s2);
                undertakeContract.setUndertakeContractCode(s2);


            }
            boolean b = this.save(undertakeContract);

            return b == true ? Result.ok() : Result.error();
    }


    @Override
    public List<UndertakeContract> generatedNumber(String un, String date, String area) {
        List<UndertakeContract> contracts = undertakeContractMapper.generatedNumber(un, date, area);

        return contracts;
    }

    @Override
    public Result updUnder(UndertakeContract undertakeContract) {

        QueryWrapper<UndertakeContract> wrapper = new QueryWrapper<>();
        wrapper.eq("undertake_contract_code",undertakeContract.getUndertakeContractCode());
        wrapper.eq("state",2);
        boolean b = this.update(undertakeContract, wrapper);
        return b == true ? Result.ok() : Result.error();
    }

    @Override
    public Result auditUnder(UndertakeContract undertakeContract) {

        QueryWrapper<UndertakeContract> wrapper = new QueryWrapper<>();
        wrapper.eq("undertake_contract_code",undertakeContract.getUndertakeContractCode());
        wrapper.eq("state",0);
        boolean b = this.update(undertakeContract, wrapper);
        return b == true ? Result.ok() : Result.error();
    }

    @Override
    public Result delUnder(String undertakeContractCode) {

        QueryWrapper<UndertakeContract> wrapper = new QueryWrapper<>();
        wrapper.eq("undertake_contract_code",undertakeContractCode);
        wrapper.eq("state",2);
        boolean b = this.remove(wrapper);
        return b == true ? Result.ok() : Result.error();
    }


}
